from fdfs_client.client import Fdfs_client
from rest_framework.decorators import action
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

# from apps.meiduo_admin.serializer.sku import
from apps.meiduo_admin.serializer.sku import SPUSpecSerializer, SPUSerializer, BrandSerializer, SpecOptionSerializer, \
    SpecSerializer, GoodsChannelSerializer, GoodsChannelGroupSerializer, BrandsSerializer
from apps.meiduo_admin.serializer.sku import SKUGoodsSerializer, CategorySerializer
from apps.meiduo_admin.utils import PageNum
from apps.goods.models import SKU, GoodsCategory, SPUSpecification, SPU, Brand, SpecificationOption, GoodsChannel, \
    GoodsChannelGroup
from meiduo_mall import settings


class SKUGoodsView(ModelViewSet):
    queryset = SKU.objects.all().order_by('id')
    serializer_class = SKUGoodsSerializer
    pagination_class = PageNum
    permission_classes = [IsAdminUser]

    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')

        if keyword is '' or keyword is None:
            return SKU.objects.all().order_by('id')
        else:
            return SKU.objects.filter(name__contains=keyword).order_by('id')

    @action(methods=['get'], detail=False)
    def categories(self, request):
        data = GoodsCategory.objects.filter(subs=None)

        ser = CategorySerializer(data, many=True)

        return Response(ser.data)

    def specs(self, request, pk):

        data = SPUSpecification.objects.filter(spu_id=pk)
        ser = SPUSpecSerializer(data, many=True)
        return Response(ser.data)


class SPUView(ModelViewSet):
    permission_classes = [IsAdminUser]
    queryset = SPU.objects.all().order_by('id')
    pagination_class = PageNum
    serializer_class = SPUSerializer

    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')

        if keyword is '' or keyword is None:
            return SPU.objects.all().order_by('id')
        else:
            return SPU.objects.filter(name__contains=keyword).order_by('id')

    def simple(self, request):
        brand = Brand.objects.all().order_by('id')
        ser = BrandSerializer(brand, many=True)
        return Response(ser.data)

    def get_categories(self, request):
        category = GoodsCategory.objects.filter(parent__isnull=True).order_by('id')
        ser = CategorySerializer(category, many=True)
        return Response(ser.data)

    def get_categories_id(self, request, pk):
        category = GoodsCategory.objects.filter(parent_id=pk).order_by('id')
        ser = CategorySerializer(category, many=True)
        return Response(ser.data)

    def images(self, request):
        client = Fdfs_client(settings.FASTDFS_PATH)
        image = request.data.get('image')
        res = client.upload_by_buffer(image.read())
        if res['Status'] != 'Upload successed.':
            return Response(status=403)
        logo_url = res['Remote file_id']
        img_url = settings.FDFS_URL + logo_url
        return Response({'img_url': img_url})


class SpecsView(ModelViewSet):
    permission_classes = [IsAdminUser]
    queryset = SpecificationOption.objects.all().order_by('id')
    pagination_class = PageNum
    serializer_class = SpecOptionSerializer

    def simple(self, request):
        specid = SPUSpecification.objects.all()
        ser = SPUSpecSerializer(specid, many=True)
        return Response(ser.data)


class ChannelsView(ModelViewSet):
    permission_classes = [IsAdminUser]
    queryset = GoodsChannel.objects.all().order_by('id')
    pagination_class = PageNum
    serializer_class = GoodsChannelSerializer

    def channel_types(self, request):
        types = GoodsChannelGroup.objects.all()
        ser = GoodsChannelGroupSerializer(types, many=True)
        return Response(ser.data)

    def categories(self, request):
        category = GoodsCategory.objects.filter(parent__isnull=True).order_by('id')
        ser = CategorySerializer(category, many=True)
        return Response(ser.data)


class BrandsView(ModelViewSet):
    permission_classes = [IsAdminUser]
    pagination_class = PageNum
    queryset = Brand.objects.all().order_by('id')
    serializer_class = BrandsSerializer
